package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author syf
 * @version 1.0
 * @date 2019/08/03 19:28
 * @Description TODO:商品SPU控制器
 */
@RestController
@RequestMapping
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> SpuPageQuery(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                          @RequestParam(value = "rows",defaultValue = "5") Integer rows,
                                                          @RequestParam(value = "key",required = false) String key,
                                                          @RequestParam(value = "saleable",required = false) Boolean saleable){
        return ResponseEntity.ok(goodsService.SpuPageQuery(page,rows,key,saleable));
    }

    /**'
     * 新增商品信息
     * @param spuDTO
     * @return
     */
    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 商品上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateById(@RequestParam("id") Long id,
                                           @RequestParam(value = "saleable",defaultValue = "true") Boolean saleable ){

        goodsService.updateById(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改商品信息
     * 查询SpuDetail接口
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> SpuDetailById(@RequestParam("id") Long id){
        return ResponseEntity.ok(goodsService.SpuDetailById(id));
    }

    /**
     * 修改商品信息
     * 根据spuID查询sku
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> skuBySpuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(goodsService.skuBySpuId(id));
    }

    /**
     * 修改商品接口
     * @param spuDTO
     * @return
     */
    @PutMapping("/goods")
    public ResponseEntity<Void>  updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //根据spuId查询spu信息
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> findSpuIdBySpu(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.findSpuIdBySpu(id));
    }

    //根据sku的id集合获取sku集合信息
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> findSkuBuSkuIds(@RequestParam("ids") List ids){
        return ResponseEntity.ok(goodsService.findSkuBuSkuIds(ids));
    }

    //减库存接口
    @PostMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(Map<Long,Integer> cartMap){
        goodsService.minusStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
